package com.navercorp.pinpoint.web.config;

import com.navercorp.pinpoint.common.server.util.AgentEventMessageDeserializer;
import com.navercorp.pinpoint.common.server.util.AgentEventMessageDeserializerV1;
import com.navercorp.pinpoint.common.server.util.ServerTraceMetadataLoaderService;
import com.navercorp.pinpoint.common.server.util.Slf4jCommonLoggerFactory;
import com.navercorp.pinpoint.common.service.DefaultServiceTypeRegistryService;
import com.navercorp.pinpoint.thrift.io.AgentEventHeaderTBaseDeserializerFactory;
import com.navercorp.pinpoint.thrift.io.CommandHeaderTBaseDeserializerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class ApplicationContextWebConfig {

    @Bean
    public Slf4jCommonLoggerFactory slf4jCommonLoggerFactory() {
        return new Slf4jCommonLoggerFactory();
    }

    @Bean
    public ServerTraceMetadataLoaderService typeLoaderService(Slf4jCommonLoggerFactory slf4jCommonLoggerFactory) {
        return new ServerTraceMetadataLoaderService(slf4jCommonLoggerFactory);
    }

    @Bean
    public DefaultServiceTypeRegistryService serviceTypeRegistryService(ServerTraceMetadataLoaderService typeLoaderService,Slf4jCommonLoggerFactory slf4jCommonLoggerFactory) {
        return new DefaultServiceTypeRegistryService(typeLoaderService,slf4jCommonLoggerFactory);
    }

    @Bean
    public CommandHeaderTBaseDeserializerFactory commandHeaderTBaseDeserializerFactory() {
        return new CommandHeaderTBaseDeserializerFactory();
    }

    @Bean
    public AgentEventHeaderTBaseDeserializerFactory agentEventHeaderTBaseDeserializerFactory() {
        return new AgentEventHeaderTBaseDeserializerFactory();
    }

    @Bean
    public AgentEventMessageDeserializer agentEventMessageDeserializer(CommandHeaderTBaseDeserializerFactory commandHeaderTBaseDeserializerFactory,
                                                                       AgentEventHeaderTBaseDeserializerFactory agentEventHeaderTBaseDeserializerFactory) {
        List list = new ArrayList<>();
        list.add(commandHeaderTBaseDeserializerFactory);
        list.add(agentEventHeaderTBaseDeserializerFactory);
        return new AgentEventMessageDeserializer(list);
    }

    @Bean
    public AgentEventMessageDeserializerV1 agentEventMessageDeserializerV1() {
        return new AgentEventMessageDeserializerV1();
    }
}
